package com.example.f8399.testdemo.activity;

import android.arch.lifecycle.Observer;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.example.f8399.testdemo.BaseActivity;
import com.example.f8399.testdemo.R;
import com.example.f8399.testdemo.databinding.ActivityBusBinding;
import com.example.f8399.testdemo.utils.LiveDataBus;
import com.jakewharton.rxbinding3.view.RxView;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import kotlin.Unit;

/**
 * 代码参考：https://github.com/JeremyLiao/LiveDataBus
 * 使用介绍：https://tech.meituan.com/Android_LiveDataBus.html
 */
public class BusActivity extends BaseActivity {

    ActivityBusBinding mBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_bus);

        RxView.clicks(mBinding.btn1)
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Unit>() {
                    @Override
                    public void accept(Unit unit) throws Exception {
                        LiveDataBus.get().with("disposable").setValue("hello,this is rxbinding and livedatabus");
                    }
                });


        LiveDataBus.get().with("disposable",String.class).observe(BusActivity.this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                mBinding.tv1.setText(s);
            }
        });

        LiveDataBus.get().with("disposable",String.class).observe(BusActivity.this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                mBinding.tv2.setText(s);
            }
        });

    }
}
